package com.example.demo.p;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2022/7/12 12:07
 */
public class Test2 {
    public static void main(String[] args) {
        String m = "2022-07-16 23:23:11";
        String m1 = "2022-07-16";
        boolean rqFormat = isRqFormat(m1);
        System.out.print(rqFormat);
    }
    /**
     * 判断字符串是否是yyyy-MM-dd格式
     * @param mes 字符串
     * @return boolean 是否是日期格式
     */
    public static boolean isRqFormat(String mes){
        String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])";
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(mes);
        if (matcher.matches()) {
            pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");
            matcher = pattern.matcher(mes);
            if (matcher.matches()) {
                int y = Integer.valueOf(matcher.group(1));
                int m = Integer.valueOf(matcher.group(2));
                int d = Integer.valueOf(matcher.group(3));
                if (d > 28) {
                    Calendar c = Calendar.getInstance();
                    c.set(y, m-1, 1);//每个月的最大天数
                    int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
                    return (lastDay >= d);
                }
            }
            return true;
        }
        return false;

    }

    /**
     * 判断字符串是否是yyyyMMddHHmmss格式
     * @param mes 字符串
     * @return boolean 是否是日期格式
     */
    public static boolean isRqSjFormat(String mes){
        String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])"
                + "([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]";
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(mes);
        if (matcher.matches()) {
            pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2}).*");
            matcher = pattern.matcher(mes);
            if (matcher.matches()) {
                int y = Integer.valueOf(matcher.group(1));
                int m = Integer.valueOf(matcher.group(2));
                int d = Integer.valueOf(matcher.group(3));
                if (d > 28) {
                    Calendar c = Calendar.getInstance();
                    c.set(y, m-1, 1);//每个月的最大天数
                    int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
                    return (lastDay >= d);
                }
            }
            return true;
        }
        return false;

    }
    /**
     * 判断字符串是否是HH-mm-ss格式
     * @param mes 字符串
     * @return boolean 是否是日期格式
     */
    public static boolean isSjFormat(String mes){
        mes = mes.replace("-","");
        if(mes.length()!=6){
            return false;
        }
        String regex="^\\d+$";
        if(!mes.matches(regex)){
            return false;
        }
        int h;
        int m;
        int s;
        try {
            h=Integer.parseInt(mes.substring(0, 2));
            m=Integer.parseInt(mes.substring(2,4));
            s=Integer.parseInt(mes.substring(4, 6));
            if(h>23||h<0||m>59||m<0||s>59||s<0){
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
